<template>
  <div id="bmsHeader">
    <el-row :gutter="24">
      <el-col :span="16">
        <el-row :gutter="24">
          <el-col :span="2">
            <transition name = "fold">
              <i class="iconfont icon-row close" :class="{open:rotate}" @click="shrinkage();" style="cursor: pointer; display: inline-block;"></i>
            </transition>
          </el-col>
          <el-col :span="22">
            <div class="breadcrumb">
              <el-breadcrumb separator-class="el-icon-arrow-right">
                <el-breadcrumb-item v-for="(item ,i) in breadRumbs" :key="i">{{item}}</el-breadcrumb-item>
              </el-breadcrumb>
            </div>
          </el-col>
        </el-row>
      </el-col>
      <el-col :span="8">
        <el-row :gutter="24">
          <el-col :span="4">
            <div @click="handleFullScreen">
              <!-- 全屏显示 -->
              <el-tooltip effect="dark" :content="fullScreen?`全屏`:`取消全屏`" placement="bottom">
                <i class="iconfont icon-quanping" v-if="fullScreen" style="font-size: 25px;"></i>
                <i class="iconfont icon-quxiaoquanping" v-else style="font-size: 25px;"></i>
              </el-tooltip>
            </div>
          </el-col>
          <el-col :span="18">
            <div class="wrap">
              <div style="margin-left: auto">
                <el-dropdown @command="handleCommand">
                  <span class="el-dropdown-link">
                    {{ userName }} <i class="iconfont icon-xiala"></i>
                  </span>
                  <el-dropdown-menu slot="dropdown" class="header-new-drop">
                    <el-dropdown-item command="userCenter" @click.native="goToPersonalCenter">
                      个人中心
                    </el-dropdown-item>
                    <el-dropdown-item command="logout" @click.native="logOut">退出登录</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </div>
              <img v-if="headImgSrc" :src="headImgSrc" class="img"/>
            </div>
          </el-col>
        </el-row>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {
  getCurrentUserInfo
} from '@/assets/request/api.js';
export default {
  name: 'bmsHeader',
  data() {
    return {
      username: '',
      rotate: false,
      fullScreen: true,
      breadRumbs: []
    }
  },
  computed: {
    userName () {
      if (this.$store.state.user.userInfo !== null) {
        return this.$store.state.user.userInfo.name
      } else {
        return ''
      }
    },
    headImgSrc () {
      if (this.$store.state.user.userInfo !== null) {
        return this.$store.state.user.userInfo.headImg
      } else {
        return ''
      }
    }
  },
  methods: {
    shrinkage(){
      this.rotate = !this.rotate;
      this.$emit('shrinkage')
    },
    // 全屏事件
    handleFullScreen() {
      // 此处可根据获取节点进行区域全屏事件
      let element = document.documentElement;
      if (!this.fullScreen) {
          if (document.exitFullscreen) {
              document.exitFullscreen();
          } else if (document.webkitCancelFullScreen) {
              document.webkitCancelFullScreen();
          } else if (document.mozCancelFullScreen) {
              document.mozCancelFullScreen();
          } else if (document.msExitFullscreen) {
              document.msExitFullscreen();
          }
      } else {
          if (element.requestFullscreen) {
              element.requestFullscreen();
          } else if (element.webkitRequestFullScreen) {
              element.webkitRequestFullScreen();
          } else if (element.mozRequestFullScreen) {
              element.mozRequestFullScreen();
          } else if (element.msRequestFullscreen) {
              // IE11
              element.msRequestFullscreen();
          }
      }
      this.fullScreen = !this.fullScreen;
    },
    handleCommand(command) {
      console.log(command)
    },
    goToPersonalCenter() {
      this.$router.push('/personalCenter')
    },
    logOut() {
      this.$store.commit('user/logout')
      this.$router.push('/login')
      this.$message({
        message: '退出成功',
        type: 'success'
      });
    },
    initBreadRumbs() {
      this.breadRumbs = this.$route.meta.breadRumbs.split(',')
    },
    async initUserInfo() {
      await getCurrentUserInfo().then(res => {
        this.$store.commit('user/setUserInfo', res.data)
      }).catch(err => {
        this.$message({
            message: err.message,
            type: "error"
        });
      })
    }
  },
  watch: {
    $route(to,from) {
      this.breadRumbs = to.meta.breadRumbs.split(',')
    }
  },
  created() {
    this.initBreadRumbs()
  },
  mounted() {  
    this.initUserInfo()
  },
}
</script>

<style scoped>

.img {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 3px solid #f2f3f5;
  background: #619fe7;
}

.wrap {
  display: flex;
  justify-content: center;
  align-items: center;
}

.el-dropdown-link {
  cursor: pointer;
  color: #409EFF;
}
.el-icon-arrow-down {
  font-size: 16px;
}


.close {
  transform: rotate(0deg);
  transition: all 0.75s ease;
}

.open {
	transform: rotate(90deg);
}

.breadcrumb {
  height: 10vh;
  display: flex;
  align-items:center;
}


.el-dropdown-menu__item:not(.is-disabled):hover {
  color: black;
  background-color: rgb(243,243,243);
}

 .el-popper[x-placement^="bottom"]{
  margin-top: -15px;
}

 
</style>
